<script lang="ts" setup>
/**
 * 用户详情-套餐调整记录
 */
import { reactive, ref, onActivated, onMounted } from 'vue'
import { FormInstance } from 'element-plus'
import { useTable } from '@/hooks/useTable'
import { useRoute } from 'vue-router'
import { queryOrderChangeLog } from '@/api/record'
import {
  queryOrderChangeLogTableCol,
  queryOrderChangeLogRequestData
} from '@/api/record/types/record'
import { getToken } from '@/utils/cache/cookies'
import { useActivated } from '@/hooks/useActivated'
import { copyEvent } from '@/utils'
import dayjs from 'dayjs'

defineOptions({
  name: 'AdjustTermRecord'
})
const route = useRoute()
const form = reactive<queryOrderChangeLogRequestData>({
  orderId: route.query.orderId ? (route.query.orderId as string) : '',
  operationType: '',
  phone: ''
})
const formRef = ref<FormInstance | null>(null)
const { pagination, handleSizeChange, handleCurrentChange, data, loading, setCurrentPage, load } =
  useTable<queryOrderChangeLogTableCol>(queryOrderChangeLog, getToken() ?? '', () => form)

const handleFormSearch = () => {
  setCurrentPage(1)

  load()
}
const reset = () => {
  formRef.value?.resetFields()
}
onActivated(() => {
  form.orderId = (route.query.orderId as string) || ''
})
/** 处理是否缓存 */
useActivated(
  form,
  { form },
  [
    { key: 'orderId', path: 'form.orderId' },
    { key: 'operationType', path: 'form.operationType' },
    { key: 'phone', path: 'form.phone' }
  ],
  load,
  []
)
onMounted(() => {
  load()
})
</script>

<template>
  <div class="xy-container">
    <el-form :model="form" inline ref="formRef">
      <el-form-item prop="phone" label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item prop="operationType" label="操作类型">
        <el-select v-model="form.operationType" style="width: 150px">
          <el-option label="不限" value="" />
          <el-option label="增加天/次数" value="0" />
          <el-option label="减少逾期天数" value="2" />
          <el-option label="扣除天数/次数" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item prop="orderId" label="订单号">
        <el-input v-model="form.orderId" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleFormSearch">查询</el-button>
        <el-button plain @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data" v-loading="loading" class="no-border">
      <el-table-column prop="mobilePhone" label="手机号" />
      <el-table-column prop="orderType" label="套餐类型" />
      <el-table-column prop="orderId" label="订单" min-width="300">
        <template #default="scope">
          <div class="flex-align-center">
            <span>{{ scope.row.orderId }}</span
            >&nbsp;
            <span @click="copyEvent(scope.row.orderId)" class="is-reference">复制</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="operationType" label="操作类型" />
      <el-table-column prop="duration" label="时长/次数" />
      <el-table-column prop="reason" label="调整原因" />
      <el-table-column prop="createdTime" label="调整时间" sortable>
        <template #default="scope">
          <span>{{ dayjs(scope.row.createdTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="operator" label="操作人" />
    </el-table>
    <div flex justify-end mt-20>
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :total="pagination.total"
        layout="total,prev,pager,next,jumper,sizes"
      />
    </div>
  </div>
</template>

<style scoped></style>
